package com.sdkx.app.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.DBHelper;
import com.sdkx.app.ViewModel.MarkManager;
import com.sdkx.app.ViewModel.TrackLineManager;
import com.sdkx.app.activitys.MainActivity;
import com.sdkx.app.dialog.markdialog.ModiMarkDialog;
import com.sdkx.app.model.Mark;
import com.sdkx.app.model.TrackLine;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TrackFragment extends BaseFragment {
    RecyclerView recyclerView;
    TrackAdapter trackAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_menu_track1, container, false);
        //返回上一级点击事件
        Button tv_all_air_spaces = (Button) view.findViewById(R.id.tv_all_air_spaces2);

        //返回上一级按钮
        tv_all_air_spaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // ((MainActivity)getActivity()).ShowMainMenu(0);
                goBack();
            }
        });
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.listvierw);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        trackAdapter = new TrackAdapter();
        recyclerView.setAdapter(trackAdapter);
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(getActivity().getDrawable(R.drawable.listview_divider));
        recyclerView.addItemDecoration(divider);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //  super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //屏幕中最后一个可见子项的position
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                //当前屏幕所看到的子项个数
                int visibleItemCount = layoutManager.getChildCount();
                //当前RecyclerView的所有子项个数
                int totalItemCount = layoutManager.getItemCount();
                //RecyclerView的滑动状态
                int state = recyclerView.getScrollState();
                if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && newState == recyclerView.SCROLL_STATE_IDLE) {
                    List<TrackLine> lines = TrackLineManager.getItemsLimit(50, totalItemCount);
                    if (lines != null && lines.size() > 0) {
                        trackAdapter.addAllList(lines);
                        Toast.makeText(getContext(), "sdfd", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.ViewHolder> {
        List<TrackLine> tracklines;

        public TrackAdapter() {
            tracklines = TrackLineManager.getItemsLimit(50, 0);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.mark_list_item, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(view);
        }

        public void addAllList(List<TrackLine> list) {
            tracklines.addAll(list);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.init(tracklines.get(position));
        }

        @Override
        public int getItemCount() {
            return tracklines.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView name, details, createTime;
            ImageView img;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
            SimpleDateFormat formattime = new SimpleDateFormat("dd HH:mm:ss");

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                details = itemView.findViewById(R.id.details);
                createTime = itemView.findViewById(R.id.latlon);
                img = itemView.findViewById(R.id.markimg);
                img.setImageResource(R.drawable.airplane_icon);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View item) {
                        final View popview = getLayoutInflater().inflate(R.layout.marklist_menu_layout, null);

                        final PopupWindow popupWindow = new PopupWindow(popview,
                                200, LinearLayout.LayoutParams.WRAP_CONTENT);

                        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        popupWindow.setFocusable(true);
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.showAsDropDown(item, item.getWidth() - 20, -(item.getHeight()), Gravity.LEFT | Gravity.TOP);
                        TextView editview = popview.findViewById(R.id.modify);
                        editview.setText("全显");
                        editview.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                popupWindow.dismiss();
                            }
                        });
                        popview.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                popupWindow.dismiss();
                            }
                        });
                        popview.findViewById(R.id.map).setVisibility(View.GONE);
                    }
                });
            }

            public void init(TrackLine line) {
                name.setText(line.getName());
                createTime.setText(format.format(new Date(line.getCreateTime())));
                details.setText("时长:" + getTime(line.getEndtime() - line.getCreateTime()));
            }

            private String getTime(long millis) {
                if(millis < 0){
                    return "0";
                }
                long sectime = millis /1000;
                int daytime  = 24*3600;
                int day = (int)(sectime/daytime);
                int hour = (int)((sectime % daytime) /3600);
                int min = (int)((sectime % 3600)/60);
                int sec = (int)(sectime % 60);
                StringBuilder builder = new StringBuilder();
                if(day > 0) {
                    builder.append(day);
                    builder.append("天");
                }
                if(hour > 0){
                    builder.append(hour);
                    builder.append("小时");
                }
                if(min > 0){
                    builder.append(min);
                    builder.append("分");
                }
                if(sec > 0){
                    builder.append(sec);
                    builder.append("秒");
                }
                return builder.toString();
            }
        }
    }
}